package com.lingying.caiwudailikehudaun.controller.piaoJuLieBiao;

import com.alibaba.fastjson.JSONObject;

import com.lingying.caiwudailikehudaun.domain.zuoZhangPiaoJu.FaPiao;
import com.lingying.caiwudailikehudaun.domain.zuoZhangPiaoJu.FilePiaoJuJiaoJieBean;
import com.lingying.caiwudailikehudaun.dto.PiaoJuJiaoJieDTO;
import com.lingying.caiwudailikehudaun.mapper.piaoJuLieBiao.PiaoJuLieBiaoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author 尹浩
 * @create 2020-08-06 17:37
 */

@Controller
@ResponseBody
@RequestMapping("/jiaojie")
@CrossOrigin(origins = "*" ,maxAge = 1800)
public class PiaoJuLieBiaoController {

    @Autowired
    PiaoJuLieBiaoMapper piaoJuLieBiaoMapper;

    @Resource
    HttpServletRequest httpServletRequest;
    //添加事务 引入platformTransactionManager对象
    @Autowired
    private PlatformTransactionManager platformTransactionManager;


    /**
     * 查询票据交接列表
     * @param limit
     * @return
     */
    @RequestMapping(value = "/queryPiaoJuJiaoJieList",method = RequestMethod.GET)
    public Object queryPiaoJuJiaoJieList(
//            @RequestParam(value = "daiLiShangCid" , required = false )Integer daiLiShangCid,
            @RequestParam(value = "companyName" , required = false )String companyName,
            @RequestParam(value = "zhaiYao" , required = false )String zhaiYao,
            @RequestParam(value = "jiaoJieType" , required = false )String jiaoJieType,
            @RequestParam(value = "sendUser" , required = false )String sendUser,
            @RequestParam(value = "sendUserName" , required = false )String sendUserName,
            @RequestParam(value = "acceptUser" , required = false )String acceptUser,
            @RequestParam(value = "acceptUserName" , required = false )String acceptUserName,
            @RequestParam(value = "jiaoJieFangShi" , required = false )String jiaoJieFangShi,
            @RequestParam(value = "tianXieUserId" , required = false )Integer tianXieUserId,
            @RequestParam(value = "tianXieRen" , required = false )String tianXieRen,
            @RequestParam(value = "page" , required = false ,defaultValue = "0") Integer page ,
            @RequestParam(value = "limit" , required = false ,defaultValue = "10") Integer limit){

        System.out.println("jiaoJieFangShi--"+jiaoJieFangShi);
        JSONObject object = new JSONObject();
        Integer pages = (page-1)*limit;
        Integer count = null;
        List<PiaoJuJiaoJieDTO> queryPiaoJuJiaoJieList = new ArrayList<>();
        Integer userId = (Integer) httpServletRequest.getSession().getAttribute("userId");
        String userName = (String) httpServletRequest.getSession().getAttribute("userName");
        Integer jiGouId = (Integer) httpServletRequest.getSession().getAttribute("jiGouId");
        List list = new ArrayList();
        if(companyName==null||companyName.equals("")){
        }else{
            list.add("公司名字："+companyName);
        }
        if(zhaiYao==null||zhaiYao.equals("")){
        }else {
            list.add("，摘要："+companyName);
        }
        if(jiaoJieType==null||jiaoJieType.equals("")){
        }else {
            list.add("，交接类型："+jiaoJieType);
        }
        if(sendUserName==null||sendUserName.equals("")){
        }else {
            list.add("，给予人："+sendUserName);
        }
        if(acceptUserName==null||acceptUserName.equals("")){
        }else {
            list.add("，接收人："+acceptUserName);
        }
        if(jiaoJieFangShi==null||jiaoJieFangShi.equals("")){
        }else {
            list.add("，交接方式："+jiaoJieFangShi);
        }
        if(tianXieRen==null||tianXieRen.equals("")){
        }else {
            list.add("，添加人："+tianXieRen);
        }

        Integer userType = (Integer) httpServletRequest.getSession().getAttribute("userType");

//        if(userType==1){
        System.err.println("管理员----------》》");
        //最高管理员查看所有票据交接列表
        count = piaoJuLieBiaoMapper.queryPiaoJuJiaoJieCount(jiGouId,zhaiYao,companyName, jiaoJieType,sendUser, acceptUser,jiaoJieFangShi, tianXieUserId);
        queryPiaoJuJiaoJieList = piaoJuLieBiaoMapper.queryPiaoJuJiaoJieList(
                jiGouId,zhaiYao , companyName , jiaoJieType,sendUser, acceptUser,jiaoJieFangShi, tianXieUserId,pages , limit);
//        }else{
//            System.err.println("普通用户----------》》");
//            //最高管理员以外查看所有自己的票据交接列表
//            count = jiaoJieMapper.queryPiaoJuJiaoJieCountByUser(userId,jiGouId,zhaiYao,companyName, jiaoJieType,sendUser, acceptUser,jiaoJieFangShi);
//            queryPiaoJuJiaoJieList = jiaoJieMapper.queryPiaoJuJiaoJieListByUser(
//                    userId,jiGouId,zhaiYao , companyName , jiaoJieType,sendUser, acceptUser,jiaoJieFangShi,pages , limit);
//        }

//        caoZuoRiZhiMapper.addBeiZhu(jiGouId,userId,"查询","做账票据交接",userName+"：根据条件："+list+"查询出了"+queryPiaoJuJiaoJieList.size()+"条数据",jiGouId);
        if(queryPiaoJuJiaoJieList.size()>0){
            object.put("data",queryPiaoJuJiaoJieList);
            object.put("code",0);
            object.put("count",count);
            object.put("msg","查询成功");
        }else{
            object.put("data",null);
            object.put("code",500);
            object.put("msg","无数据");
        }
        return  object;
    }

    /**
     * 根据文件id删除，（修改票据为删除的状态）暂时不用了
     * @param fileId
     * @return
     */
    @RequestMapping(value = "/delPiaoJu" , method = RequestMethod.POST)
    public Object delPiaoJu(Integer fileId){
        JSONObject object = new JSONObject();
        Integer count = piaoJuLieBiaoMapper.delPiaoJu(fileId);
        if(count>0){
            object.put("msg","删除成功");
            object.put("code",200);
        }else{
            object.put("msg","删除失败");
            object.put("code",200);
        }
        return object;
    }

    /**
     * 根据添加交接 下拉选择的公司名字查询相对应的发票信息
     * @param companyName
     * @return
     * @throws Exception
     */
    @GetMapping("/queryFaPiaoByCompanyName")
    public Object queryFaPiaoByCompanyName(String companyName){
        JSONObject object=new JSONObject();
        List<Map> map= piaoJuLieBiaoMapper.queryFaPiaoByCompanyName(companyName);
        String urla="http:// lingyingsms.oss-cn-beijing.aliyuncs.com/";
        if (map.size()>0){
            for (Map map1 : map) {
                map1.put("ossPath",urla+map1.get("ossPath").toString());
                Integer faPiaoId=Integer.parseInt(map1.get("faPiaoId").toString());
                Integer fuJianId= piaoJuLieBiaoMapper.getUploadFileId(faPiaoId);
                map1.put("fuJianId",fuJianId);
            }
        }
        if (map.size()>0){
            object.put("code",200);
            object.put("msg","有数据");
            object.put("map",map);
        }else {
            object.put("code",500);
            object.put("msg","无数据");
        }
        return object;
    }

    /**
     * 查询增值税里边的文件 MD5值
     * @throws Exception
     */
    @GetMapping("/fileMd5List")
    public Object fileMd5List(){
        JSONObject object=new JSONObject();
        List<String> fileMd5List= piaoJuLieBiaoMapper.fileMd5List();
        if (fileMd5List.size()>0){
            object.put("code",200);
            object.put("msg","有数据");
            object.put("data",fileMd5List);
        }else {
            object.put("code",500);
            object.put("msg","无数据");
        }
        return object;
    }

    /**
     * 根据票据交接Id查询 交接里边的发票
     * @param piaoJuJiaoJieId
     * @return
     */
    @PostMapping("/queryPiaoJuList")
    public Object queryPiaoJuList (Integer piaoJuJiaoJieId){
        JSONObject object = new JSONObject();
        System.out.println("piaoJuJiaoJieId:"+piaoJuJiaoJieId);
        List<Map<String , Object>> queryPiaoJuList = piaoJuLieBiaoMapper.queryPiaoJuList(piaoJuJiaoJieId);
        System.out.println("queryPiaoJuList---"+queryPiaoJuList);
        if(queryPiaoJuList.size()>0){
            object.put("code",200);
            object.put("data",queryPiaoJuList);
            object.put("msg","查询成功");
        }else{
            object.put("code",500);
            object.put("msg","暂无数据");
        }
        return  object;
    }


    /**
     * 模糊查询票据列表数据（交接的所有发票）
     * 桑满 20210603
     * @param piaoZhongId
     * @param AmountInFiguers
     * @param suoShuQi
     * @param gouHuoFangName
     * @param xiaoHuoFangName
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/piaoJuList")
    public Object piaoJuList(
            @RequestParam(value = "piaoZhongId" , required = false )Integer piaoZhongId,
            @RequestParam(value = "AmountInFiguers" , required = false )String AmountInFiguers,
            @RequestParam(value = "suoShuQi" , required = false )String suoShuQi,
            @RequestParam(value = "gouHuoFangName" , required = false )String gouHuoFangName,
            @RequestParam(value = "xiaoHuoFangName" , required = false )String xiaoHuoFangName,
            @RequestParam(value = "startSuoShuQi" , required = false )String startSuoShuQi,
            @RequestParam(value = "endSuoShuQi" , required = false )String endSuoShuQi,
            @RequestParam(value = "xianShiZhuangTai" , required = false )Integer xianShiZhuangTai,
            @RequestParam(value = "page" , required = false ,defaultValue = "0") Integer page ,
            @RequestParam(value = "limit" , required = false ,defaultValue = "10") Integer limit){

        JSONObject object = new JSONObject();
        Integer daiLiShangCid = (Integer) httpServletRequest.getSession().getAttribute("daiLiShangCid");
        System.err.println("piaoZhongId========>>>>>"+piaoZhongId);
        System.err.println("AmountInFiguers========>>>>>"+AmountInFiguers);
        System.err.println("suoShuQi========>>>>>"+suoShuQi);
        System.err.println("gouHuoFangName========>>>>>"+gouHuoFangName);
        System.err.println("xiaoHuoFangName========>>>>>"+xiaoHuoFangName);

        Integer pages = (page-1)*limit;
        List<Map<String,Object>> zuoZhangShenBaoList = new ArrayList<>();
        Integer piaoJuListCount = 0;
//        if(userType==1){
        //最高管理员查询所有的票据
        zuoZhangShenBaoList = piaoJuLieBiaoMapper.piaoJuList(daiLiShangCid,piaoZhongId,
                AmountInFiguers,suoShuQi,gouHuoFangName,
                xiaoHuoFangName,startSuoShuQi,endSuoShuQi,xianShiZhuangTai,pages,limit);
        //最高管理员查询所有的票据总条数
        piaoJuListCount = piaoJuLieBiaoMapper.piaoJuListCount(daiLiShangCid,piaoZhongId,
                AmountInFiguers,suoShuQi,gouHuoFangName,
                xiaoHuoFangName,startSuoShuQi,endSuoShuQi,xianShiZhuangTai);
        if(zuoZhangShenBaoList.size()>0){
            object.put("code",0);
            object.put("data",zuoZhangShenBaoList);
            object.put("count",piaoJuListCount);
            object.put("msg","查询成功");
        }else{
            object.put("code",500);
            object.put("msg","无");
        }
        return object;
    }

    /**
     * 查询票据交接的 销购货方
     * 桑满20210603
     * @return
     */
    @PostMapping("/queryXiaoGouHuoFangList")
    public Object queryXiaoGouHuoFangList(){

        JSONObject object = new JSONObject();
        Integer daiLiShangCid = (Integer) httpServletRequest.getSession().getAttribute("daiLiShangCid");
        //购货方集合
        List<FilePiaoJuJiaoJieBean> GouHuoFangList = piaoJuLieBiaoMapper.queryGouHuoFangList(daiLiShangCid);
        //销货方
        List<FilePiaoJuJiaoJieBean> XiaoHuoFangList = piaoJuLieBiaoMapper.queryXiaoHuoFangList(daiLiShangCid);
        //票种集合
        List<FaPiao> PiaoZhong = piaoJuLieBiaoMapper.queryPiaoZhong(daiLiShangCid);
        if(GouHuoFangList.size()>0&&XiaoHuoFangList.size()>0){
            object.put("GouHuoFangList",GouHuoFangList);
            object.put("XiaoHuoFangList",XiaoHuoFangList);
            object.put("PiaoZhong",PiaoZhong);
            object.put("code",200);
            object.put("msg","查询成功");
            return object;
        }
        object.put("code",500);
        object.put("msg","查询失败");
        return object;
    }

    /**
     * 根据发票id查询 发票信息 发票路径，销购货方，税额，
     * 不含税金额，含税金额，开票日期 用途
     * @param faPiaoId
     * @return
     */
    @GetMapping("/faPiaoMap")
    public Object faPiaoMap(Integer faPiaoId){
        JSONObject object = new JSONObject();
        Integer daiLiShangCid = (Integer)httpServletRequest.getSession().getAttribute("daiLiShangCid");
        System.out.println(daiLiShangCid+"----"+daiLiShangCid);
        Map<String,Object> faPiaoMap = piaoJuLieBiaoMapper.faPiaoMap(daiLiShangCid,faPiaoId);
        System.out.println("faPiaoMap"+faPiaoMap);
        if(faPiaoMap!=null){
            object.put("code",200);
            object.put("data",faPiaoMap);
            object.put("msg","查询成功");
        }else{
            object.put("code",500);
            object.put("msg","无");
        }
        return object;
    }
}
